import 'package:flutter/material.dart';
import 'package:flutter_app/api/api.dart' as api;

class LoginPage extends StatefulWidget {
  @override
  LoginPageState createState() {
    return new LoginPageState();
  }

  const LoginPage();
}

class LoginPageState extends State<LoginPage> {
  var _username;
  var _password;

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: AppBar(
        title: Text("登陆中心"),
      ),
      body: new Container(
        alignment: Alignment.topLeft,
        margin: const EdgeInsets.symmetric(horizontal: 8.0),
        child: Column(children: <Widget>[
          Container(
            margin: const EdgeInsets.only(
                left: 10.0, right: 10.0, bottom: 10.0, top: 40.0),
            child: TextField(
              maxLines: 1,
              decoration: new InputDecoration(
                hintText: '用户名',
                border: InputBorder.none,
              ),
              onChanged: (s) {
                setState(() {
                  _username = s;
                });
              },
            ),
            color: new Color(0xFFF3F5F7),
            padding: const EdgeInsets.only(left: 20.0, right: 10.0),
          ),
          Container(
            margin: const EdgeInsets.all(10.0),
            child: TextField(
              maxLines: 1,
              decoration: new InputDecoration(
                hintText: '密码',
                border: InputBorder.none,
              ),
              onChanged: (s) {
                setState(() {
                  _password = s;
                });
              },
            ),
            color: new Color(0xFFF3F5F7),
            padding: const EdgeInsets.only(left: 20.0, right: 10.0),
          ),
          GestureDetector(
            child: Container(
              child: Text("登录"),
              width: 200.0,
              height: 40.0,
              alignment: Alignment.center,
              margin: const EdgeInsets.only(top: 90.0),
              decoration: BoxDecoration(
                borderRadius: new BorderRadius.circular(3.0),
                color: Colors.cyan,
              ),
            ),
            onTap: _login,
          ),
        ]),
      ),
      backgroundColor: Colors.white,
    );
  }

  void _login() {
    api.login(_onLogin, _username, _password);
  }

  void _onLogin(dynamic obj) {
    Navigator.of(context).pop(obj);
  }
}
